package handler

import (
	"2212b/examination/api/handler/request"
	"2212b/examination/internal/dao"
	"2212b/examination/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddExamination(c *gin.Context) {
	var req request.AddExaminationReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数信息失败",
			"Data": err.Error(),
		})
		return
	}
	dao.Create(&model.Exam{
		ExamName:         req.ExamName,
		ExamIntroduction: req.ExamIntroduction,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
		"Data": true,
	})
}

func ShouExamination(c *gin.Context) {
	var req request.ShouExaminationReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数信息失败",
			"Data": err.Error(),
		})
		return
	}
	var exam model.Exam

	if exam.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "该权限查看详情失败",
			"Data": nil,
		})
		return
	}
	if dao.GetOneById(req.Id, &exam) {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "不存在",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "详情成功",
		"Data": true,
	})
}

func UpdateExamination(c *gin.Context) {
	var req request.UpdateExaminationReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数信息失败",
			"Data": err.Error(),
		})
		return
	}
	var exam model.Exam

	if exam.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "该权限修改失败",
			"Data": nil,
		})
		return
	}
	if dao.GetOneById(req.Id, &exam) {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "不存在",
			"Data": nil,
		})
		return
	}
	dao.Update(&model.Exam{
		Id:               req.Id,
		ExamName:         req.ExamName,
		ExamIntroduction: req.ExamIntroduction,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"Data": true,
	})
}

func RemoveExamination(c *gin.Context) {
	var req request.RemoveExaminationReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 100000,
			"msg":  "参数信息失败",
			"Data": err.Error(),
		})
		return
	}
	//var exam model.Exam

	//if exam.UserId != c.GetUint("userId") {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 100000,
	//		"msg":  "该权限删除失败",
	//		"Data": nil,
	//	})
	//	return
	//}
	//if dao.GetOneById(req.Id, &exam) {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 100000,
	//		"msg":  "不存在",
	//		"Data": nil,
	//	})
	//	return
	//}
	dao.Delete(&model.Exam{
		Id: req.Id,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除成功",
		"Data": true,
	})
}

func ListExamination(context *gin.Context) {
	
}

func Update(context *gin.Context) {
	
}